package com.asman.worker.rn

import androidx.fragment.app.FragmentActivity
import com.asman.worker.dialog.ServicePhoneDialogFragment
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import java.util.*

/**
 * author : ybk
 * date   : 2019-07-18 19:47
 * desc   :
 */
class NativeMakeCall(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

    override fun getName(): String {
        return "NativeMakeCall"
    }

    override fun getConstants(): Map<String, Any>? {
        return HashMap()
    }

    /**
     * 拨打电话 弹出dialog
     * @param phoneNum 手机号码字符串
     * @return -1：点击取消按钮，1：点击拨打按钮
     */
    @ReactMethod
    fun show(phoneNum: String, promise: Promise) {
        if (currentActivity is FragmentActivity) {
            ServicePhoneDialogFragment.newInstance(phoneNumber = phoneNum)
                    .show((currentActivity as FragmentActivity).supportFragmentManager, null, object : ServicePhoneDialogFragment.CallBack {
                        override fun onCancelClick() {
                            promise.resolve(-1)
                        }

                        override fun onOkClick() {
                            promise.resolve(1)
                        }
                    })
        }
    }
}
